# encoding: utf-8
"""
UCloud resources
============
"""

import logging

from flask_restplus import Resource

from app.extensions.api import Namespace, http_exceptions
from app.modules.common.heathcheck import BasicServiceCheck, AppServiceCheck

log = logging.getLogger(__name__) # pylint: disable=invalid-name
api = Namespace('vcloud', description="vcloud") # pylint: disable=invalid-name

from . import parameters, schemas
from .models import VCloud
from app.modules.common.utils import Converter
from .platform import VPlatform


@api.route('/stat')
class UClouds(Resource):

    @api.response(schemas.BaseVCloudSchema())
    def get(self):
        """
        List of teams.

        Returns a list of teams starting from ``offset`` limited by ``limit``
        parameter.
        """

        try:
            log.info('=================start read information of os =============')

            platform = VPlatform()

            hostname = platform.hostname()

            ipaddress = platform.ipaddress()

            meminfo = platform.meminfo()

            duskstat = platform.disk_stat('/')

            cpu_cores = platform.cpu_stat_cores()

            cpu_free = platform.cpu_free()

            df_h = platform.disk_free()
            log.info('=================df_h:%s', df_h)

            data = VCloud(ipaddress=ipaddress,
                          hostname=hostname,
                          status='running',
                          cpu_cores=cpu_cores,
                          memory=meminfo['MemTotal'],
                          memory_free=meminfo['MemFree'],
                          disk=str(duskstat['total']) + ' MB',
                          disk_free=str(duskstat['free']) + ' MB',
                          disk_free_list=df_h,
                          cpu_free=str(cpu_free)+' %')
        except BaseException as e:
            log.error('================ read information of os error, %s', e)

        log.info('=================end read information of os =============')
        return Converter.obj_to_dict(data)




@api.route('/heathcheck')
class UClouds(Resource):

    @api.response(schemas.BaseVCloudSchema())
    def get(self):
        """
        List of teams.

        Returns a list of teams starting from ``offset`` limited by ``limit``
        parameter.
        """

        try:
            log.info('=================start read information of os =============')

            platform = VPlatform()

            hostname = platform.hostname()

            ipaddress = platform.ipaddress()

            meminfo = platform.meminfo()

            duskstat = platform.disk_stat('/')

            cpu_cores = platform.cpu_stat_cores()

            df_h = platform.disk_free()
            log.info('=================df_h:%s', df_h)

            data = VCloud(ipaddress=ipaddress,
                          hostname=hostname,
                          status='running',
                          cpu_cores=cpu_cores,
                          memory=meminfo['MemTotal'],
                          memory_free=meminfo['MemFree'],
                          disk=str(duskstat['total']) + ' MB',
                          disk_free=str(duskstat['free']) + ' MB',
                          disk_free_list=df_h)
        except BaseException as e:
            log.error('================ read information of os error, %s', e)

        log.info('=================end read information of os =============')
        return Converter.obj_to_dict(data)


@api.route('/serviceCheck')
class VCloudServiceCheck(Resource):

    @api.parameters(parameters.ServiceCheck())
    @api.response(code=http_exceptions.Conflict.code)
    def get(self, args):
        results = dict()

        zk = BasicServiceCheck.check_zookeeper()
        if len(zk) > 0:
            results['zoodeeper'] = zk
        else:
            results['zoodeeper'] = True

        redis = BasicServiceCheck.check_redis()
        if len(redis) > 0:
            results['redis'] = redis
        else:
            results['redis'] = True

        results['rabbitmq'] = BasicServiceCheck.check_rabbitmq()

        results['mysql'] = BasicServiceCheck.check_mysql()

        results['oracle'] = BasicServiceCheck.check_oracle()

        results['mongodb'] = BasicServiceCheck.check_mongodb()

        results['cassandra'] = BasicServiceCheck.check_cassandra()

        empty = AppServiceCheck.check_app_dir()
        if len(empty) > 0:
            results['app_empty'] = empty
        else:
            results['app_empty'] = True

        return results

